﻿using System;
using System.Linq;

using AmazeUI.db;
using AmazeUI.Imodel;

namespace AmazeUI.bll.Open
{
    public class OpenBll : BaseBll
    {




        /// <summary>
        /// 查看微信用户是否存在
        /// </summary>
        /// <returns></returns>
        public OpenUser IsWxUser(string openid)
        {
            return this.db.OpenUser.FirstOrDefault(a => a.openid == openid);

        }

        /// <summary>
        /// 写入微信用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="xtUserid"></param>
        /// <returns></returns>
        public int SetWxUser(IOpenUser model, int xtUserid)
        {
            var wxUser = new OpenUser(model);
            var ou = this.db.OpenUser.FirstOrDefault(a => a.openid == model.openid);
            if (ou != null)
            {
                return ou.Id;
            }
            wxUser.XtUserId = xtUserid;
            this.db.OpenUser.Add(wxUser);
            this.db.SaveChanges();

            return wxUser.Id;
        }

        /// <summary>
        ///  获取open用户信息
        /// </summary>  
        /// <param name="xtuserid"></param>
        /// <returns></returns>
        public OpenUser GetOpenUser(int xtuserid)
        {

            return this.db.OpenUser.FirstOrDefault(a => a.XtUserId == xtuserid);


        }
    }
}